package com.lanyuan.controller.api;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.WinUserFormMap;
import com.lanyuan.mapper.WinUserMapper;
import com.lanyuan.thrid.wechat.utils.Api;
import com.lanyuan.thrid.wechat.utils.Constants;
import com.lanyuan.util.Common;

/**
 * 
 * @author lanyuan 2014-11-19
 * @Email: mmm333zzz520@163.com
 * @version 3.0v
 */
@Controller
@RequestMapping("/api/wxfrom/")
public class WxformController extends BaseController {

	@Inject
	private WinUserMapper winuserMapper;

	@ResponseBody
	@RequestMapping("editwinuser")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "用户管理-修改用户")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public boolean editEntity() throws Exception {
		WinUserFormMap winuserFormMap = winuserMapper.findbyFrist("id", getPara("id"), WinUserFormMap.class);
		if ((winuserFormMap != null) && (winuserFormMap.getInt("status") == 0)) {
			winuserFormMap.put("mobile", getPara("mobile"));
			winuserFormMap.put("status", "1");
			winuserMapper.editEntity(winuserFormMap);
			return true;
		}
		return false;
	}

	@RequestMapping("winuserlist")
	public String winuserlist(String code, Model model) throws Exception {
		// 获取code，以获取openid
		Map<String, String> result = Api.oauth2AccessToken(code);
		if (StringUtils.isEmpty(result.get("openid"))) {
			return "redirect:" + Api.connectOauth2Authorize(Constants.MY_ACTIVITY_URL, false);
		}

		String openid = result.get("openid");

		WinUserFormMap winuserFormMap = getFormMap(WinUserFormMap.class);
		winuserFormMap = toFormMap(winuserFormMap, "1", "6", winuserFormMap.getStr("orderby"));
		winuserFormMap.put("openid", openid);
		winuserFormMap.put("column", "createTime");
		winuserFormMap.put("sort", "desc");
		List<WinUserFormMap> winuserdata = winuserMapper.findUserPage(winuserFormMap);// 不调用默认分页,调用自已的mapper中findUserPage
		model.addAttribute("winuser", winuserdata); // 当前活动信息
		return Common.BACKGROUND_PATH + "/wx/myactivity";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			WinUserFormMap winuserFormMap = new WinUserFormMap();
			winuserFormMap = winuserMapper.findbyFrist("id", id, WinUserFormMap.class);
			model.addAttribute("retult", winuserFormMap);
		}
		return Common.BACKGROUND_PATH + "/wx/result";
	}

}
